<?php
namespace Home\Controller;
use Think\Controller;

class GrandController extends CommonController {
	public function  index(){
		$this -> assign('$title','会员等级管理');
	
		$mod = M('users');
		$user = $mod ->join('zx_user_type on zx_users.user_type=zx_user_type.id')-> where('user_id = "%s "',$_SESSION['users']['user_id']) -> find();
		
		$this -> assign('user',$user);
		$_SESSION['users']['user_type'] = $user['user_type'];		
		//用户升级日志
		$logMod = M('account_log');
		$user_update_log = $logMod -> field('change_desc,about_user_name,user_money2,change_time') ->where("user_id = '%d' and change_type='%s' ",$_SESSION['users']['user_id'],'70')->order('change_time desc')->select();
		
		$this -> assign('log',$user_update_log);
		$this -> display('');
	}

	//升级用户等级
	public function update(){
		$userMod = M('users');
		$userMod -> startTrans();
			$res = $userMod -> field('is_out,is_jh,name,user_id,user_money3,user_type,user_money2,tree_code,tree_left_code,tree_right_code,user_name,inviter_id,report_user_id') -> join('zx_user_type on zx_users.reg_type=zx_user_type.id') -> where('user_id=%d',[$_SESSION['users']['user_id']]) -> find();

				if($res['user_money2']>=I('post.jg')*100){
					$xuyao = I('post.jg')*100;
					// echo $xuyao;
					$user_money2 = $res['user_money2']-$xuyao;
					if($res['user_money3']<=0){
						$res1 = M() -> execute('update zx_users set huitian_time='.time().',user_money2='.$user_money2.',user_type='.I('post.jg').',reg_type='.I('post.jg').' where user_id='.$res['user_id']);
					}else{
						$res1 = M() -> execute('update zx_users set user_money2='.$user_money2.',user_type='.I('post.jg').',reg_type='.I('post.jg').' where user_id='.$res['user_id']);
					}
					
					$user_type_update = M('user_type')->field('name')->where('id = "%s" ',I('post.jg'))->find();
					
					//添加日志
					$logMod = M('account_log');
					$log_update['user_id'] = $_SESSION['users']['user_id'];
					$log_update['user_money2'] = -I('post.jg')*100;
					$log_update['change_type'] = '70';
					$log_update['change_desc'] = '会员升级：'.$res['name'].'->'.$user_type_update['name'];
					$log_update['change_time'] = time();
					
					$res2 = $logMod ->add($log_update);
				
					$param = M('param') -> field('param3') -> where('id=%d',[1]) -> find();
					$jd = M('user_type') -> field('jd_level') -> select();
					foreach($jd as $k=>$v){
						$arr[] = $v['jd_level'];
					}
					rsort($arr);
					$max = $arr[0];

					//算左区的业绩
					if(!empty($_SESSION['users']['tree_left_code'])){
						$left = M() -> execute('update zx_users set tree_num=tree_num+'.$xuyao.',left_num=left_num+'.$xuyao.',left_leave_amount=left_leave_amount+'.$xuyao.' where is_out=1 and is_jh=0 and user_id in('.$res['tree_left_code'].')'); 
					}else{
						$left = true;
					}
					//算右区的业绩
					if(!empty($_SESSION['users']['tree_right_code'])){
						$right = M() -> execute('update zx_users set tree_num=tree_num+'.$xuyao.',right_num=right_num+'.$xuyao.',right_leave_amount=right_leave_amount+'.$xuyao.' where is_out=1 and is_jh=0 and user_id in('.$res['tree_right_code'].')');
						// echo M() -> _SQL();
					}else{
						$right = true;
					}

					//推荐人
					if($res['inviter_id']==3000001){
						$xiaoshou=true;
					}else{
						$inviter = M('users') -> field('is_out,is_jh,user_id,zt_percent') -> join('zx_user_type on zx_users.reg_type=zx_user_type.id') -> where('user_id=%d',[$res['inviter_id']]) -> find();

						// //保单中心扣除积分
						// $baodan = M() -> execute('update zx_users set user_money2=user_money2-'.$xuyao.' where user_id='.$res['repost_user_id']);
						// //加入积分明细
						// $data1 = ['user_id'=>$res['repost_user_id'],'user_money2'=>((-1)*($res['reg_amount'])),'change_type'=>5,'change_time'=>time(),'change_desc'=>'升级会员'.$res['user_name'].'扣除'.$res['reg_amount'].'注册积分','about_user_name'=>$res['user_id']];
						// $mingxi = M('account_log') -> data($data1) -> add();
						//销售奖
						if($inviter['is_out'] == 1 && $inviter['is_jh'] ==0)
						$xsj = $xuyao*$inviter['zt_percent']/100;

						$xiaoshou = M() -> execute('update zx_users set pay_points1=pay_points1+'.$xsj.',user_money1=user_money1+'.$xsj.' where user_id='.$res['inviter_id']);

						$xsj1 = ['change_time'=>time(),'user_id'=>$res['inviter_id'],'pay_points1'=>$xsj,'user_money1'=>$xsj,'change_type'=>99,'change_desc'=>'销售奖：【升级'.$res['user_name'].'会员ID：'.$res['user_id'].'】'.$xuyao.'*'.$inviter['zt_percent'].'%='.$xsj,'about_user_name'=>$res['user_id']];

						$res10 = M('account_log') -> data($xsj1) -> add();

					}
					
					//服务奖
					$baodanzhongxin = M('users') -> field('is_out,is_jh') -> where('user_id=%d',[$res['report_user_id']]) -> find();
					if($baodanzhongxin['is_out'] == 1 && $baodanzhongxin['is_jh'] == 0){
						$fuwujiang = $xuyao*$param['param3']/100;
						$fuwujiang1 = ['change_time'=>time(),'user_id'=>$res['report_user_id'],'user_money1'=>$fuwujiang,'change_type'=>99,'pay_points5'=>$fuwujiang,'change_desc'=>'服务奖：【升级：'.$res['user_name'].'会员ID:'.$res['user_id'].'】'.$xuyao.'*'.$param['param3'].'%='.$fuwujiang,'about_user_name'=>$res['user_id']];
						M('account_log') -> data($fuwujiang1) -> add();
						$fuwu = M() -> execute('update zx_users set pay_points5=pay_points5+'.$fuwujiang.',user_money1=user_money1+'.$fuwujiang.' where user_id='.$res['report_user_id']);
					}
					
					// echo M() -> _SQL();
					//平衡奖
					$dp = M() -> query('select tree_id,inviter_id,user_id,left_leave_amount,right_leave_amount,max_dp_amount,user_type from zx_users where is_out=1 and is_jh=0 and user_id in('.$res['tree_code'].')');


					foreach($dp as $k=>$v){
						if($v['left_leave_amount']==0){
							$k = true;
						}elseif($v['right_leave_amount']==0){
							$k = true;
						}elseif($v['left_leave_amount']>=$v['right_leave_amount']){
							$jian = $v['left_leave_amount'] - $v['right_leave_amount'];

							$res5 = M() -> execute('update zx_users set left_leave_amount='.$jian.',right_leave_amount=0'.' where user_id='.$v['user_id']);

							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
							//算出接点人要拿的平衡奖
							$dpjx = $v['right_leave_amount']*$user_type['dp_percent']/100;
							$dpj = $dpjx + $v['max_dp_amount'];
							if($dpj>$user_type['dp_max_amount']){
								$dpj = $user_type['dp_max_amount'];
								$dpjx = $user_type['dp_max_amount']-$v['max_dp_amount'];
								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【升级'.$res['user_name'].'会员ID：'.$res['user_id'].',由于当日封顶,获得'.$dpjx,'about_user_name'=>$res['user_id']];
								M('account_log') -> data($data2) -> add();
							}else{
								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【升级'.$res['user_name'].'会员ID：'.$res['user_id'].'】'.$v['right_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>$res['user_id']];
								M('account_log') -> data($data2) -> add();
							}
							
							$userdp = M('users') -> field('is_out,is_jh,inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
							//计算代数奖
							for($i=1;$i<=$max;$i++){
								// echo $i;
								// dump($user['inviter_id']);
								if(empty($userdp['user_id'])){
									$k = true;
								}elseif($userdp['user_id'] ==0){
									$k = true;
								}elseif($userdp['user_id']==3000001){
									$k = true;
								}elseif($userdp['is_out']==0||$userdp['is_jh']==1){
									$k = true;
									$userdp = M('users') -> field('is_out,is_jh,inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$userdp['inviter_id']]) -> find();
								}else{
									if($i<=$userdp['jd_level']){
										$dsj = $dpjx*$userdp['jd_percent']/100;
										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$userdp['user_id']);
										$data3 = ['change_time'=>time(),'user_id'=>$userdp['user_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【升级'.$res['user_name'].'会员ID：'.$res['user_id'].'】'.$dpjx.'*'.$userdp['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];
										M('account_log') -> data($data3) -> add();
										$userdp = M('users') -> field('inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$userdp['inviter_id']]) -> find();
									}
								}
							}
						}elseif($v['left_leave_amount']<$v['right_leave_amount']){
							$jian = $v['right_leave_amount'] - $v['left_leave_amount'];
							$res5 = M() -> execute('update zx_users set right_leave_amount='.$jian.',left_leave_amount=0'.' where user_id='.$v['user_id']);
							$user_type = M('user_type') -> where('id=%d',[$v['user_type']]) -> find();
							//算出接点人要拿的平衡奖
							$dpjx = $v['left_leave_amount']*$user_type['dp_percent']/100;
							$dpj = $dpjx + $v['max_dp_amount'];
							if($dpj>$user_type['dp_max_amount']){
								$dpj = $user_type['dp_max_amount'];
								$dpjx = $user_type['dp_max_amount']-$v['max_dp_amount'];
								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【升级'.$res['user_name'].'会员ID：'.$res['user_id'].'】,由于当日封顶,获得'.$dpjx,'about_user_name'=>$res['user_id']];
								// $user = M('users') -> field('inviter_id') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
								M('account_log') -> data($data2) -> add();
							}else{
								$res6 = M() -> execute('update zx_users set max_dp_amount='.$dpj.',user_money1=user_money1+'.$dpjx.',pay_points2=pay_points2+'.$dpjx.' where user_id='.$v['user_id']);
								$data2 = ['change_time'=>time(),'user_id'=>$v['user_id'],'user_money1'=>$dpjx,'pay_points2'=>$dpjx,'change_type'=>99,'change_desc'=>'平衡奖：【升级'.$res['user_name'].'会员ID：'.$res['user_id'].'】'.$v['left_leave_amount'].'*'.$user_type['dp_percent'].'%='.$dpjx,'about_user_name'=>$res['user_id']];
								// $user = M('users') -> field('inviter_id') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
								M('account_log') -> data($data2) -> add();
							}
							
							$userdp = M('users') -> field('is_out,is_jh,inviter_id,user_id,jd_level,jd_percent') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$v['inviter_id']]) -> find();
							//计算代数奖
							for($i=1;$i<=$max;$i++){
								if(empty($userdp['user_id']) || $user['inviter_id'] ==0){
									$k = true;
								}elseif($userdp['user_id'] ==0){
									$k = true;
								}elseif($userdp['user_id']==3000001){
									$k = true;
								}elseif($userdp['is_out']==0||$user['is_jh']==1){
									$k = true;
									$userdp = M('users') -> field('is_out,is_jh,inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$userdp['inviter_id']]) -> find();
								}else{
									if($i<=$userdp['jd_level']){
										$dsj = $jpjx*$userdp['jd_percent']/100;
										$res7 = M() -> execute('update zx_users set pay_points3=pay_points3+'.$dsj.',user_money1=user_money1+'.$dsj.' where user_id='.$userdp['user_id']);
										$data3 = ['change_time'=>time(),'user_id'=>$user['inviter_id'],'user_money1'=>$dsj,'pay_points3'=>$dsj,'change_type'=>99,'change_desc'=>'第'.$i.'代管理奖【激活'.$res['user_name'].'会员ID：'.$res['user_id'].'】'.$dpjx.'*'.$userdp['jd_percent'].'%='.$dsj,'about_user_name'=>$v['user_id']];

										M('account_log') -> data($data3) -> add();
										// echo M() -> _SQL();
										$userdp = M('users') -> field('is_out,is_jh,inviter_id,jd_percent,user_id,jd_percent,jd_level') -> join('zx_user_type on zx_users.user_type=zx_user_type.id') -> where('user_id=%d',[$userdp['inviter_id']]) -> find();
									}
									
								}
							}
						}

					}
					//计算被激活用户购买的积分
					$shengji = M('user_type') -> field('pay_amount') -> where('id=%d',[I('post.jg')]) -> find();
					$buy_num =$xuyao*$shengji['pay_amount']/100;
					// $buy_num = $buy_num+$res['user_money3'];
					//更新日志
					$rizhi = M('account_log') -> data(['user_id'=>$res['user_id'],'user_money3'=>$buy_num,'change_type'=>60,'change_time'=>time(),'change_desc'=>'升级自动获得认购积分']) -> add();
					//激活的用户的认购积分
					$user1 = M() -> execute('update zx_users set user_money3=user_money3+'.$buy_num.' where user_id='.$res['user_id']);


					if($xiaoshou&&$fuwu&&$user1){
						$userMod -> commit();
						$this -> success('升级成功,请稍等....',U('home/grand/index'));
					}else{
						$userMod -> rollback();
						$this -> error('升级失败,请核对信息后再次申请',U('home/grand/index'));
					}
					
				}else{
					$this -> error('抱歉,您的注册积分不足',U('home/grand/index'));
				}
			
				
	}

	public function updateAjax(){
		if(md5(I('post.repassword')) == $_SESSION['users']['repassword']){
			echo '1';
		}else{
			echo '2';
		}
	}
	public function downAjax(){
		if(md5(I('post.repassword')) == $_SESSION['users']['repassword']){
			echo '1';
		}else{
			echo '2';
		}
	}
	public function down(){
		
		$data['user_type'] = I('post.jjg');
		$data['reg_type'] = I('post.jjg');
		$userMod = M('users');
		$res = $userMod -> where("user_id = '%s' ", $_SESSION['users']['user_id'])->data($data)->save();
		if($res>0){					
			$this -> success('降级成功,请稍等....',U('home/grand/index'));
		}else{
			$this -> error('降级失败');
					
		}
	}






}

